package com.RuiJi.controller;

import com.RuiJi.entity.AddressBook;
import com.RuiJi.service.AddressBookService;
import com.RuiJi.utils.BaseContext;
import com.RuiJi.utils.R;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Twinkle
 * @PACKAGE_NAME: com.RuiJi.controller
 * @NAME: AddressBookController
 * @USER: Twinkle
 * @DATE: 2023/4/17
 * @TIME: 20:44
 * @YEAR: 2023
 * @MONTH: 04
 */
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    /**
     * @description: 新增
     * @author: Twinkle
     * @date: 2023/4/18 19:36
     * @param: [addressBook]
     * @return: com.RuiJi.utils.R<com.RuiJi.entity.AddressBook>
     **/
    @PostMapping
    public R<AddressBook> save(@RequestBody AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBookService.save(addressBook);
        return R.success(addressBook);
    }


    /**
     * @description:设置默认地址
     * @author: Twinkle
     * @date: 2023/4/18 19:41
     * @param: [addressBook]
     * @return: com.RuiJi.utils.R<com.RuiJi.entity.AddressBook>
     **/
    @PutMapping("/default")
    public R<AddressBook> setDefault(@RequestBody AddressBook addressBook) {
        LambdaUpdateWrapper<AddressBook> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId());
        wrapper.set(AddressBook::getIsDefault, 0);
        addressBookService.update(wrapper);
        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);
        return R.success(addressBook);
    }

    /**
     * @description:查询默认地址信息
     * @author: Twinkle
     * @date: 2023/4/18 19:45
     * @param: []
     * @return: com.RuiJi.utils.R<com.RuiJi.entity.AddressBook>
     **/
    @GetMapping("/default")
    public R<AddressBook> getDefaul() {
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId());
        queryWrapper.eq(AddressBook::getIsDefault, 1);
        AddressBook addressBook = addressBookService.getOne(queryWrapper);
        if (null == addressBook) {
            return R.error("未找到该对象");
        } else {
            return R.success(addressBook);
        }
    }

    /**
     * @description:地址列表
     * @author: Twinkle
     * @date: 2023/4/18 19:49
     * @param: [addressBook]
     * @return: com.RuiJi.utils.R<java.util.List < com.RuiJi.entity.AddressBook>>
     **/
    @GetMapping("/list")
    public R<List<AddressBook>> list(AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(null != addressBook.getUserId(), AddressBook::getUserId, addressBook.getUserId());
        queryWrapper.orderByDesc(AddressBook::getUpdateTime);
        return R.success(addressBookService.list(queryWrapper));
    }

    /**
     * @description:根据ID回显数据
     * @author: Twinkle
     * @date: 2023/4/18 21:48
     * @param: [id]
     * @return: com.RuiJi.utils.R<com.RuiJi.entity.AddressBook>
     **/
    @GetMapping("/{id}")
    public R<AddressBook> edit(@PathVariable Long id){

        AddressBook byId = addressBookService.getById(id);
        return R.success(byId);
    }

    /**
     * @description:更新地址
     * @author: Twinkle
     * @date: 2023/4/18 21:48
     * @param: [addressBook]
     * @return: com.RuiJi.utils.R<java.lang.String>
     **/
    @PutMapping
    public R<String>edit(@RequestBody AddressBook addressBook){
        addressBookService.updateById(addressBook);
        return R.success("更新成功");
    }

    /**
     * @description:删除地址
     * @author: Twinkle
     * @date: 2023/4/18 21:49
     * @param: [ids]
     * @return: com.RuiJi.utils.R<java.lang.String>
     **/
    @DeleteMapping
    public R<String>delete(@PathVariable List <Long> ids){
        addressBookService.removeByIds(ids);
        return R.success("删除成功");
    }
}
